<template>
  <div>
      <p>标题：<input type="text" v-model="title"></p>
      <p>内容：<input type="text" v-model="content"></p>
      <p><button @click="add">添加</button></p>
      <div>
          <div v-show="is_comment">
                <textarea :placeholder="msg" v-model="comment">

            </textarea>
            <button @click="add_comment">提交</button>
          </div>

          <table style="margin:0 auto;">
              <tr v-for="i in news">
                  <td>{{i.title}}</td>
                  <td>{{i.content}}</td>
                  <td><button @click="can_comment(i.id,i.title)">评论</button></td>
                  <td><button @click="show(i.id)">查看</button></td>
              </tr>
          </table>
      </div>
  </div>

</template>

<script>
import axios from 'axios'
export default {
    data(){
        return{
            title:'',
            content:'',
            uid : sessionStorage.getItem('uid'),
            news:[],
            is_comment:0,
            msg : '',
            comment:'',
            news_id:0,
            
        }
    },
    created(){
        this.get_news()
    },
    methods:{
        show(id){
            this.$router.push({path:'/detail',query:{news_id:id}})
        },
        add_comment(){
            var formdata = new FormData()
            formdata.append('content',this.comment)
            formdata.append('user_id',this.uid)
            formdata.append('news_id',this.news_id)
            axios.post('http://127.0.0.1:8000/myapp/add_comment/',formdata
            ).then(res=>{
                console.log(res.data)   
                if(res.data.code==200){
                    alert(res.data.msg)
                }
            })
        },
        can_comment(id,str){
            axios.get('http://127.0.0.1:8000/myapp/add_comment/',{
                params:{uid:this.uid}
            }
            ).then(res=>{
                console.log(res.data)   
                if(res.data.code==403){
                    window.location.href = '/login'
                }else{
                    this.is_comment = 1
                    this.msg = '评论：'+str
                    this.news_id = id
                }
            })
        },
        add(){
            var formdata = new FormData()
            formdata.append('title',this.title)
            formdata.append('content',this.content)
            formdata.append('user_id',this.uid)

            axios.post('http://127.0.0.1:8000/myapp/add_news/',formdata
            ).then(res=>{
                console.log(res.data)   
                if(res.data.code==200){
                    alert('添加成功')
                }
            })
        },
        get_news(){
            axios.get('http://127.0.0.1:8000/myapp/add_news/',
            ).then(res=>{
                console.log(res.data)   
                if(res.data.code==200){
                    // alert('添加成功')
                    this.news = res.data.data
                }
            })
        }
    }
}
</script>

<style>

</style>